package com.oasystem.service.system;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.oasystem.dao.jdbc.system.FundClassifyDAO;
import com.oasystem.dao.jdbc.user.UserDao;
import com.oasystem.domain.PagingBean;
import com.oasystem.domain.fund.FundClassification;
import com.oasystem.domain.user.User;
import com.oasystem.exceptions.ClassifyAlreadyExistException;
import com.oasystem.utils.PrimaryKeyGenerator;

@Transactional(readOnly = true)
public class SystemService {

	private FundClassifyDAO fundClassifyDAO;
	private UserDao userDAO;

	public void setFundClassifyDAO(FundClassifyDAO fundClassifyDAO) {
		this.fundClassifyDAO = fundClassifyDAO;
	}

	public List<FundClassification> fundClassifyList() {
		return this.fundClassifyDAO.fundClassifyList();
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void saveClassify(String classifyName)
			throws ClassifyAlreadyExistException {
		if (fundClassifyDAO.isClassifyExist(classifyName)) {
			throw new ClassifyAlreadyExistException();
		} else {
			fundClassifyDAO.save(classifyName);
		}
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void deleteClassify(String classifyId) {
		 this.fundClassifyDAO.deleteClassify(classifyId);
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void updateClassify(String classifyId, String newClassify) {
		 this.fundClassifyDAO.updateClassify(classifyId,newClassify);
	}

	public void userList(PagingBean<User> pagingBean) {
		userDAO.userList(pagingBean);
	}

	public void setUserDAO(UserDao userDAO) {
		this.userDAO = userDAO;
	}

	public User loadUser(String userid) {
		return this.userDAO.findUserByUserId(userid);
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void saveUser(User user) {
		user.setId(PrimaryKeyGenerator.getUUID());
		user.getPermission().setPermissionID(PrimaryKeyGenerator.getUUID());
		this.userDAO.saveUser(user);
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void updateUser(User user) {
		this.userDAO.updateUser(user);
	}

	@Transactional(readOnly = false, propagation = Propagation.SUPPORTS, rollbackFor = { Exception.class })
	public void deleteUser(String userid) {
		this.userDAO.deleteUser(userid);
	}

	public List<User> leaderList() {
		return this.userDAO.leaderList();
	}

}